<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="${classname}">
    <#assign instance = "${classname?lower_case}">
    <#assign table = "${tableMetaData.tableName}">

    <typeAlias alias="${instance}" type="com.unis.model.${module}.${classname}" />
    <sql id="${classname}.queryParams">
      1=1
      <isNotEmpty property="sidx">
      ORDER BY $sidx$ $sord$
      </isNotEmpty>
    </sql>
    <select id="queryAsList" parameterClass="${instance}" resultClass="${instance}">
      <isEqual property="paging" compareValue="true">
        SELECT * FROM (
          SELECT row_.*, rownum rownum_ FROM (
            SELECT * FROM ${table} where <include refid="${classname}.queryParams"/>
          ) row_ WHERE rownum &lt;= #limit#
        ) where rownum_ &gt; #start#
      </isEqual>
      <isEqual property="paging" compareValue="false">
        SELECT * FROM ${table}
      </isEqual>
    </select>
    <select id="queryAsObject" parameterClass="${instance}" resultClass="${instance}">
      SELECT * FROM ${table} WHERE 1=1
      <#list tableMetaData.primaryKeys as pk>
      <isNotEmpty property="${pk}">
      and ${pk}=#${pk}#
      </isNotEmpty>
      </#list>
    </select>
    <select id="queryRecordsCount" parameterClass="${instance}" resultClass="${instance}">
      SELECT count(*) totalCount FROM (
        SELECT * FROM ${table} where <include refid="${classname}.queryParams"/>
      )
    </select>
    <delete id="delete" parameterClass="${instance}">
      DELETE FROM ${table} WHERE 1=1
      <#list tableMetaData.primaryKeys as pk>
      <isNotEmpty property="id">
      and ${pk}=#id#
      </isNotEmpty>
      </#list>
    </delete>
    <update id="update" parameterClass="${instance}">
      UPDATE ${table} SET 
      <#list tableMetaData.columns as columnMetaData>
      <#if tableMetaData.primaryKeys?seq_contains(columnMetaData.columnName)>
      <#else>
        ${columnMetaData.columnName}=#${columnMetaData.columnName}#<#if columnMetaData_has_next>,</#if>
      </#if>
      </#list>
      WHERE 1=1
      <#list tableMetaData.primaryKeys as pk>
      <isNotEmpty property="${pk}">
      and ${pk}=#${pk}#
      </isNotEmpty>
      </#list>
    </update>
    <insert id="insert" parameterClass="${instance}">
      INSERT INTO ${table}(
      <#list tableMetaData.columns as columnMetaData>
        ${columnMetaData.columnName}<#if columnMetaData_has_next>,</#if>
      </#list>
      ) VALUES(
      <#list tableMetaData.columns as columnMetaData>
        #${columnMetaData.columnName}#<#if columnMetaData_has_next>,</#if>
      </#list>
      )
    </insert>
</sqlMap>